package com.sy.service.impl;

import com.sy.config.RedisKeyConfig;
import com.sy.config.SystemConfig;
import com.sy.dao.UserDao;
import com.sy.dao.UserInfoDao;
import com.sy.dao.UserlogDao;
import com.sy.dto.UserDto;
import com.sy.entity.User;
import com.sy.entity.Userlog;
import com.sy.etype.UserLogType;
import com.sy.service.intf.UserService;
import com.sy.third.JedisUtil;
import com.sy.util.DateUtil;
import com.sy.util.EncryptUtil;
import com.sy.util.JwtUtil;
import com.sy.util.StrUtil;
import com.sy.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @description: 码起 自动生成代码
 * @author: 小毛
 * @create: 2020-11-25 22:00:26
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private UserlogDao userlogDao;

    @Override
    public R save(User user) {
        if (userDao.insert(user) > 0) {
            return R.ok();
        } else {
            return R.fail("新增失败");
        }
    }

    @Override
    public R delById(int id) {
        if (userDao.deleteById(id) > 0) {
            return R.ok();
        } else {
            return R.fail("删除失败");
        }
    }

    @Override
    public R all() {
        return R.ok(userDao.all());
    }

    @Override
    public R checkPhone(String phone) {
        User user = userDao.selectByPhone(phone);
        if (user == null) {
            return R.ok();
        } else {
            return R.fail("亲，改手机号已被注册，可以找回密码！");
        }
    }

    @Override
    public R register(UserDto dto) {
        //1.参数验证
        if (null != dto) {
            if (StrUtil.checkNoEmpty(dto.getPassword(), dto.getPhone())) {
                //2.检验手机号
                if (checkPhone(dto.getPhone()) == null) {
                    //3.密码转换为密文同时构建用户对象
                    User user = new User(dto.getPhone(), EncryptUtil.aesenc(SystemConfig.PASS_KEY, dto.getPassword()));
                    //4.检验新增是否完成
                    if (userDao.insertId(user) > 0) {
                        //5.初始化用户详细信息并且记录
                        userInfoDao.insertInit(user.getId());
                        userlogDao.insert(new Userlog(user.getId(), UserLogType.注册.getVal(), "新用户注册，手机号：" + dto.getPhone()));
                        return R.ok();
                    }
                }
            }
        }
        return R.fail("亲，注册失败，请检查网络");
    }

    @Override
    public R findPass(UserDto dto) {
        dto.setPassword(EncryptUtil.aesenc(SystemConfig.PASS_KEY, dto.getPassword()));
        if (userDao.update(dto) > 0) {
            return R.ok();
        } else {
            return R.fail("亲，密码找回失败，请联系客服");
        }
    }

    @Override
    public R login(UserDto dto) {
        //1.校验参数
        if (null != dto) {
            if (StrUtil.checkNoEmpty(dto.getPhone(), dto.getPassword())) {
                //唯一登陆 生成令牌 存储令牌
                User user = userDao.selectByPhone(dto.getPhone());
                if (user != null) {
                    //校验密码
                    if (user.getPassword().equals(EncryptUtil.aesenc(SystemConfig.PASS_KEY, dto.getPassword()))) {
                        //2.检验当前账号有没有下线
                        if (JedisUtil.getInstance().ishave(RedisKeyConfig.AUTH_PHONE + dto.getPhone())) {
                            //存在 当前账号一在线 生成挤掉信息
                            //获取在线的令牌
                            String tk = JedisUtil.getInstance().getStr(RedisKeyConfig.AUTH_PHONE + dto.getPhone());
                            //存储挤掉信息
                            JedisUtil.getInstance().addHash(RedisKeyConfig.AUTH_OUT, tk, "在" + DateUtil.getTime());
                            //原来的令牌失效
                            JedisUtil.getInstance().delKeys(RedisKeyConfig.AUTH_PHONE + tk);
                        }
                        //生成令牌 用户id
                        String token = JwtUtil.createToken(user.getId() + "");
                        //存储令牌
                        //记录在线手机号
                        JedisUtil.getInstance().addStrEx(RedisKeyConfig.AUTH_PHONE + user.getPhone(), token, RedisKeyConfig.AUTH_TIME);
                        //记录登陆成功的令牌和对应的用户信息
                        JedisUtil.getInstance().addStrEx(RedisKeyConfig.AUTH_PHONE + token, user.getId() + "", RedisKeyConfig.AUTH_TIME);
                        return R.ok(token);
                    }
                }
            }
        }
        return R.fail("亲，账号或密码不正确");
    }
}
